<?php
/**
 *
 * Author: TheSmile
 * Date: 2023/5/15 15:37
 * Email: <594652523@qq.com>
 */

namespace App\Console\Commands;

use App\Http\Repositories\Redis\CacheDataRedis;
use App\Models\Course;
use App\Models\CourseDuty;
use App\Models\CourseDutyTeacher;
use App\Models\Message;
use App\Models\Users;
use App\Models\UserScoreRecord;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TeacherSignScore extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'teacher_sign:score';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '教师签到增加积分';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $giveScore = CacheDataRedis::getRedisInstance()->systemConfig('basic.sign_score') ?? 0;
        if (empty($giveScore)) {//设置签到送积分为0时不处理
            Log::info('平台未设置签到送积分,不做处理');
            return;
        }
        //赠送积分给上课签到和下课签到同时打卡有记录的
        $query = CourseDutyTeacher::where('need_give', 1)->orderBy('course_section_id');
        $query->chunk(100, function ($list) use ($giveScore) {
            foreach ($list as $item) {
                try {
                    DB::beginTransaction();
                    //更新赠送状态
                    CourseDutyTeacher::where('teacher_id', $item['teacher_id'])->where('course_section_id', $item['course_section_id'])->update(['need_give' => 2]);
                    //增加积分记录，业务id暂用course_section_id
                    $userInfo = Users::where('teacher_id', $item['teacher_id'])->first();
                    UserScoreRecord::create(['user_id' => $userInfo['id'], 'qty' => $giveScore, 'buss_type' => 2, 'buss_id' => $item['course_section_id'], 'remark' => '签到赠送积分']);
                    //增加教师上完课积分（签到成功默认为上完课）
                    $teacherScore = Course::where('id', $item['course_id'])->value('teacher_score');
                    UserScoreRecord::create(['user_id' => $userInfo['id'], 'qty' => $teacherScore, 'buss_type' => 2, 'buss_id' => $item['course_section_id'], 'remark' => '上课完成积分']);
                    //增加用户积分
                    Users::where('id', $userInfo['id'])->increment('score', $giveScore + $teacherScore);
                    Message::create(['user_id' => $userInfo['id'], 'title' => '积分变动提醒', 'content' => '上课签到或授完课获得' . ($giveScore + $teacherScore) . '积分']);
                    DB::commit();
                    Log::info('教师' . $item['teacher_id'] . ',章节' . $item['course_section_id'] . '处理成功');
                } catch (\Exception $e) {
                    DB::rollBack();
                    Log::error('教师签到处理失败,错误信息'.$e->getMessage());
                    Log::error('教师签到处理失败，teacher_id:' . $item['teacher_id'] . 'course_section_id:' . $item['course_section_id']);
                }

            }
        });
    }

}
